package com.ruoyi.gxs.service.impl;

import java.util.*;

import com.ruoyi.gxs.domain.GxsComprehensive;
import com.ruoyi.gxs.mapper.GxsComprehensiveMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gxs.mapper.GxsYearnameMapper;
import com.ruoyi.gxs.domain.GxsYearname;
import com.ruoyi.gxs.service.IGxsYearnameService;

/**
 * 学期名称Service业务层处理
 * 
 * @author 高晓松
 * @date 2023-12-27
 */
@Service
public class GxsYearnameServiceImpl implements IGxsYearnameService 
{
    @Autowired
    private GxsYearnameMapper gxsYearnameMapper;

    @Autowired
    private GxsComprehensiveMapper gxsComprehensiveMapper;

    /**
     * 查询学期名称
     * 
     * @param gxsYearid 学期名称主键
     * @return 学期名称
     */
    @Override
    public GxsYearname selectGxsYearnameByGxsYearid(Long gxsYearid)
    {
        return gxsYearnameMapper.selectGxsYearnameByGxsYearid(gxsYearid);
    }

    /**
     * 查询学期名称列表
     * 
     * @param gxsYearname 学期名称
     * @return 学期名称
     */
    @Override
    public List<GxsYearname> selectGxsYearnameList(GxsYearname gxsYearname)
    {
        return gxsYearnameMapper.selectGxsYearnameList(gxsYearname);
    }

    /**
     * 新增学期名称
     * 
     * @param gxsYearname 学期名称
     * @return 结果
     */
    @Override
    public int insertGxsYearname(GxsYearname gxsYearname)
    {
        GxsComprehensive comprehensive = new GxsComprehensive();
        List<GxsComprehensive> gxsComprehensives = gxsComprehensiveMapper.selectGxsComprehensiveList(comprehensive);

        gxsYearnameMapper.insertGxsYearname(gxsYearname);

        Map<String, Integer> userNameCountMap = new HashMap<>();
        for (GxsComprehensive gxsComprehensive : gxsComprehensives) {
            String userName = gxsComprehensive.getUserName();
            userNameCountMap.put(userName, userNameCountMap.getOrDefault(userName, 0) + 1);
        }

        List<GxsComprehensive> result = new ArrayList<>();
        for (GxsComprehensive gxsComprehensive : gxsComprehensives) {
            String userName = gxsComprehensive.getUserName();
            if (userNameCountMap.get(userName) < 4) {
                result.add(gxsComprehensive);
            }
        }

        Set<String> processedUserNames = new HashSet<>();
        for (GxsComprehensive com : result){
            List<GxsComprehensive> comprehensives = gxsComprehensiveMapper.selectGxsComprehensiveList(com);
            GxsComprehensive comOld = comprehensives.get(0);

            if (!processedUserNames.contains(comOld.getUserName())) {
                GxsComprehensive comNew = new GxsComprehensive();
                comNew.setUserName(comOld.getUserName());
                comNew.setNickName(comOld.getNickName());
                comNew.setUserId(comOld.getUserId());
                comNew.setDeptId(comOld.getDeptId());
                comNew.setGxsYear(gxsYearname.getGxsYearid());
                comNew.setGxsSchoolrecord(0L);
                comNew.setGxsFail(0L);
                comNew.setGxsThought(0L);
                comNew.setGxsSportsscore(0L);
                comNew.setGxsAppraisalScore(0L);

                gxsComprehensiveMapper.insertGxsComprehensive(comNew);
                processedUserNames.add(comOld.getUserName());
            }
        }


        return 1;
    }

    /**
     * 修改学期名称
     * 
     * @param gxsYearname 学期名称
     * @return 结果
     */
    @Override
    public int updateGxsYearname(GxsYearname gxsYearname)
    {
        return gxsYearnameMapper.updateGxsYearname(gxsYearname);
    }

    /**
     * 批量删除学期名称
     * 
     * @param gxsYearids 需要删除的学期名称主键
     * @return 结果
     */
    @Override
    public int deleteGxsYearnameByGxsYearids(Long[] gxsYearids)
    {
        return gxsYearnameMapper.deleteGxsYearnameByGxsYearids(gxsYearids);
    }

    /**
     * 删除学期名称信息
     * 
     * @param gxsYearid 学期名称主键
     * @return 结果
     */
    @Override
    public int deleteGxsYearnameByGxsYearid(Long gxsYearid)
    {
        return gxsYearnameMapper.deleteGxsYearnameByGxsYearid(gxsYearid);
    }

    @Override
    public GxsYearname selectYearnameByYearname(String Yearname) {
        return gxsYearnameMapper.selectYearnameByYearname(Yearname);
    }
}
